package com.demo.file.conteoller;

import com.demo.auth.annotation.PassToken;
import com.demo.utils.ObjectUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

/**
 * @author : YU.TAN
 * @date : 2021/11/8 11:39
 */
@RestController
@RequestMapping("/api/upload")
@Slf4j
public class FileController {

    private String baseUploadPath = "C:\\Users\\谭羽\\Desktop\\新建文件夹";
//    private String baseUploadPath = "src/main/resources/file";
    /**
     * 上传文件类型
     */
    private static final int VIDEO_FILE = 1;
    private static final int IMAGE_FILE = 2;
    private static final int OTHER_FILE = 3;

    private static final Set<String> videoTypes = new HashSet<String>() {
        {
            add("video/mp4");
            add("video/x-ms-wmv");
            add("video/mpeg4");
            add("video/avi");
            add("video/mpeg");
            add("video/3gp");
        }
    };

    @PassToken
    @RequestMapping(value = "/uploadFileType/{uploadFileType}", method = RequestMethod.POST)
    public String uploadFile(@RequestParam("file") MultipartFile file, @PathVariable("uploadFileType") int uploadFileType) {
        String contentType = file.getContentType();
        String fileName = file.getOriginalFilename();
        String suffix = "." + FilenameUtils.getExtension(fileName);
        String result = null;

        if (file.getSize() > 500 * 1024 * 1024) {
            return "视频大小不能超过500MB";
        }
        switch (uploadFileType) {
            case VIDEO_FILE:
                result = beforeUploadVideo(contentType, fileName);
                break;
            case IMAGE_FILE:
                result = beforeUploadImage(suffix);
                break;
            case OTHER_FILE:
                result = beforeUploadOtherFile(fileName);
                break;
            default:
                return "类型不存在";
        }

        if (ObjectUtils.isNotEmpty(result)) {
            try {
                String basePath = baseUploadPath + result;
                File filePath = new File(basePath);
                if (!filePath.getParentFile().exists()) {
                    filePath.getParentFile().mkdirs();
                }
                file.transferTo(filePath);

                return result;
            } catch (IOException e) {
                e.printStackTrace();
                return "文件上传失败";
            }
        }
        return ";";
    }

    private String beforeUploadVideo(String contentType, String fileName) {
        if (videoTypes.contains(contentType)) {
            return "/" + ObjectUtils.generateFileByTime() + "/videos/" + ObjectUtils.generateUuId() + "/" + fileName;
        }
        return null;
    }

    private String beforeUploadImage(String suffix) {
        return "/" + ObjectUtils.generateFileByTime() + "/images/" + ObjectUtils.generateUuId() + suffix;

    }

    private String beforeUploadOtherFile(String fileName) {
        return "/" + ObjectUtils.generateFileByTime() + "/others/" + ObjectUtils.generateUuId() + "/" + fileName;
    }
}
